﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UserInterface
{
    using UserControls;
    using Views;
    using Helpers;
    using Presenters;
    using Events;
    using Model;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region variables

        private static double titleHeight = //SystemParameters.WindowCaptionHeight
                        SystemParameters.ResizeFrameHorizontalBorderHeight;
        private static double verticalBorderWidth = SystemParameters.ResizeFrameVerticalBorderWidth;

        private GameInterfaceCoordinator Coordinator;

        internal static PlayerMap CurrentPlayerMap;

        #endregion

        #region constructor

        public MainWindow()
        {
            InitializeComponent();
            Coordinator = new GameInterfaceCoordinator(this.PlayerMap);
            CurrentPlayerMap = this.PlayerMap;
        }
          
        #endregion

        #region gen new size

        private Size GetNewSize(Size size)
        {           
            size.Height -= titleHeight;
            size.Width -= verticalBorderWidth;
            return size;
        }

        #endregion


        #region command events

        #region game event

        private void NewGame_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            this.Coordinator.InitGame();
        }

        private void LoadGame_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Not implemented.");
        }

        private void SaveGame_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Not implemented");
        }

        private void NextTurn_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            this.Coordinator.NextTurn();
        }

        private void ExitGame_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            this.Close();           
        }

        #endregion

        #region unit

        private void UnitMove_Executed(object sender, ExecutedRoutedEventArgs e) 
        {
            this.Coordinator.UnitMoveMode(e.Parameter);
        }

        private void UnitFortify_Executed(object sender, ExecutedRoutedEventArgs e)
        { 
            this.Coordinator.UnitFortify(e.Parameter as GameLibrary.Unit.UnitInfo); 
        }
       
        private void UnitAttack_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            this.Coordinator.UnitAttackMode(e.Parameter);
        }
    
        private void UnitBuild_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            this.Coordinator.BuildSettlement(e.Parameter as GameLibrary.Unit.UnitInfo);
        }
       
        private void UnitDisband_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            this.Coordinator.DisbandUnit(e.Parameter as GameLibrary.Unit.UnitInfo);
        }

        private void UnitExplore_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Not implemented.");       
        }

        private void UnitUpgrade_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.UnitUpgradeMode(e.Parameter as GameLibrary.Unit.UnitInfo);
        }

        #endregion

        #region information

        private void ShowUnitInfo_Executed(object sender, ExecutedRoutedEventArgs e) 
        {
            var unit = e.Parameter as GameLibrary.Unit.UnitInfo;
            Coordinator.ShowUnitInfo(unit);
        }
        private void ShowUnits_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.ShowUnits();
        } 
        private void ShowSettlementInfo_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var settlement = e.Parameter as GameLibrary.Settlement.SettlementInfo;
            Coordinator.ShowSettlementInfo(settlement);
        }
        private void ShowSettlements_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.ShowSettlements();
        }
        private void ShowRelationships_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.ShowRelationships();
        }
        private void ShowTemplates_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.ShowTemplates();
        }       
        private void ShowTileInfo_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.ShowTileInfo(e.Parameter as GameLibrary.Tile.TileType);
        }

        #endregion

        #region settlement

        private void Abandon_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            this.Coordinator.Abandon(e.Parameter as GameLibrary.Settlement.SettlementInfo);
        }

        private void BuildRoad_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.BuildBuilding(e.Parameter as GameLibrary.Settlement.SettlementInfo,
                                    "Road");
        }

        private void BuildPlant_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.BuildBuilding(e.Parameter as GameLibrary.Settlement.SettlementInfo,
                                        "Plant");
        }

        private void BuildFactory_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.BuildBuilding(e.Parameter as GameLibrary.Settlement.SettlementInfo,
                                        "Plant");
        }

        private void ChangeType_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.ChangeType(e.Parameter as GameLibrary.Settlement.SettlementInfo);
        }

        private void BuildUnit_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.CityBuildNewUnitMode(e.Parameter as GameLibrary.Settlement.SettlementInfo);
        }

        private void LinkToCity_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.LinkToCityMode(e.Parameter);
        }

        #endregion

        #region diplomatic

        private void DeclearWar_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.DeclearWar();
        }

        #endregion

        #region template

        private void CreateTemplate_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.CreatTemplate();
        }

        private void DeleteTemplate_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("delete template not support in this version.");
        }

        #endregion

        #endregion

        private void ShowPlayerInfo_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.ShowPlayerInfo();
        }

        private void ShowAbout_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Coordinator.ShowAbout();
        }
    }
}
